package log;

import java.io.File;
import java.io.FileWriter;

/**
 * Implements the logging system of ImHere.
 * @author Alexandre, Cristovao, Raul, Rodolpho, Tales
 * @version 1.1
 * @milestone Number 2
 */
public class Log {
	
	private Registrable service;
	private final String FILE_TYPE = ".log";
	
	/**
	 * Constructor of the class. It register the type of the logging.
	 * @param service that will uses the logging system
	 */
	public Log(Registrable service) {
		this.service = service;
	}
	
	/**
	 * Changes the type of the service that will use the logging system
	 * @param service - the type of the service
	 */
	public void setService(Registrable service){
		this.service = service;
	}
	
	/**
	 * Get the type of the service that uses currently the logging.
	 * @return service type
	 */
	public String getService(){
		return service.getService();
	}
	
	/**
	 * Get the error message if any problem occur when log in the file.
	 * @return success message
	 */
	public String getErrorMessage() {
		return service.getErrorMessage();
	}

	/**
	 * Get the success message if no problem occur when log in the file.
	 * @return
	 */
	public String getSuccessMessage() {
		return service.getSuccessMessage();
	}
	
	/**
	 * Writes a file with all the content of the log. 
	 * @param message that will be write in the file.
	 * @return success message - if no problem occurs.
	 *         error message - if any problem occurs.
	 */
	public String addLog(String message){
		try{
			FileWriter file = new FileWriter(new File(getService() + FILE_TYPE), true);
			file.write(message);
			file.close();
			return getSuccessMessage();
		}catch(Exception ex){
			return getErrorMessage();
		}
	}
	

}
